import {Component, ViewChild, Output, EventEmitter} from '@angular/core';
import {PaginatorForm} from "../../../models/paginatorForm";
import {DatePipe} from "@angular/common";;
import {UserService} from "../../service/user.service";
import {PromptService} from "../../../util/prompt/prompt.service";
import {RoleTypePipe} from "../../../util/pipe/rolelType.pipe";
import {CommonService} from "../../../util/common.service";
import {UserSelectModalResult} from "../../model/userSelectModalResult";
import {FilterForm} from "../../../models/filterForm";
import {FilterConditionFormTypeEnum} from "../../../models/filterConditionFormTypeEnum";
import {FilterConditionFormLeftTypeEnum} from "../../../models/filterConditionFormLeftTypeEnum";
import {FilterConditionForm} from "../../../models/filterConditionForm";
declare var $: any;
@Component({
    moduleId: module.id,
    selector: 'userSelectModal-component',
    templateUrl: "./userSelectModal.html"
})


export class UserSelectModalComponent {

    @ViewChild("userListTable") userListTable: any;
    @ViewChild("editUserSelectModal") editUserSelectModal: any;
    @ViewChild("searchUserModal") searchUserModal: any;
    selectedUser: any[] = [];
    @Output() callback = new EventEmitter<UserSelectModalResult>();
    filterForm: FilterForm = new FilterForm(
        [
            new FilterConditionForm("username", "like", FilterConditionFormTypeEnum.common)
            // new FilterConditionForm("collectionMarketMappingUserList", "eager", FilterConditionFormTypeEnum.common)
        ]
    )


    constructor(private userService: UserService, private promptService: PromptService, private commonService: CommonService) {
    }

    ngOnInit() {
        this.getList()
    }

    getList() {
        $(this.userListTable.nativeElement).bootstrapTable({
            url: this.commonService.getRequestProUrl() + "/api/user/get_filler_paginatorList",
            dataType: "json",
            method: "post",
            pagination: true, //分页
            sidePagination: "server", //服务端处理分页
            queryParams: (params: any)=> {
                return {
                    limit: params.limit,   //页面大小
                    offset: params.offset,
                    filterConditions: this.filterForm.operateSearchData()
                }
            },
            columns: [
                {
                    title: '用户名',
                    field: 'username',
                }, {
                    title: '所属采集单位',
                    field: 'collectionMarketMappingUserList',
                    formatter: (value: any, row: any, index: any) => {
                        let result: any = ""
                        for (let userMapping of value) {
                            result += '<span class="label label-info" style="margin-right: 5px">' + userMapping.market.name + '</span>'
                        }
                        return result;
                    }
                }, {
                    title: '操作',
                    field: 'operate',
                    formatter: (value: any, row: any, index: any) => {
                        let result: any = ""
                        var style = this.commonService.getTableOperateBtnCss()
                        result += '<span  title="选择" class="select  fa fa-check-circle" style="' + style + '"></span>'
                        return result;
                    },
                    events: {
                        'click .select': (e: any, value: any, row: any, index: any)=> {
                            let flag = false;
                            for (let user of this.selectedUser) {
                                if (user.id == row.id) {
                                    flag = true
                                }
                            }
                            if (!flag) {
                                this.selectedUser.push(row)
                            } else {
                                this.promptService.warning("此用户你已选择")
                            }

                        }
                    }
                }],
            responseHandler: (res: any) => {
                console.log(res)
                if (!res.success) {
                    this.promptService.warning(res.error.message)
                }
                return {
                    "total": res.paginator.total,
                    "rows": res.result   //数据
                };
            }
        })
    }

    removeSelectedUser(user: any) {
        this.selectedUser.splice(this.selectedUser.indexOf(user), 1)
    }

    showModal(userList: any) {
        this.selectedUser = userList;
        $(this.userListTable.nativeElement).bootstrapTable("refresh");
        this.commonService.showModal(this.editUserSelectModal);
    }

    showSearchModal() {
        $(this.searchUserModal.nativeElement).modal("show");
    }

    initUserSearch() {
        this.filterForm.initSearchData();
        this.searchUser();
    }

    searchUser() {
        $(this.searchUserModal.nativeElement).modal("hide");
        $(this.userListTable.nativeElement).bootstrapTable("refresh");
    }

    save() {
        let userSelectModalResult: UserSelectModalResult = new UserSelectModalResult();
        userSelectModalResult.userList = this.selectedUser;
        this.commonService.hideModal(this.editUserSelectModal);
        this.callback.emit(userSelectModalResult)
    }

}